﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Web;
using System.Web.Services;
using System.Web.Services.Description;
using System.Web.Services.Protocols;
using System.Configuration;
using cz.uhul.DSMLv2;
using cz.uhul.DSMLv2.Server.Sql;
using System.Xml.Serialization;
using System.Xml.Schema;


namespace cz.uhul.services.DSMLv2
{


  /// <summary>
  /// LD connection is SOAP WebService endpoint that can read/process DSML batchRequests
  /// against SQL LD database
  /// </summary>
  [WebService(Namespace = "https://eagri.cz/ssl/basic-app/uhul/wtest/LDS/")]
  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  [System.ComponentModel.ToolboxItem(false)]
  //[MessageContract(IsWrapped = false)]
  /// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  /// [System.Web.Script.Services.ScriptService]
  public class LdConnection : System.Web.Services.WebService
  {





    ///// <summary>
    ///// Processes DSML request on current LD server
    ///// </summary>
    ///// <param name="dsmlRequest"><c>XmlDocument</c>with valid DSML batchRequest message</param>
    ///// <returns><c>XmlDocument with DSML batchResponse from server</c></returns>
    //[WebMethod(MessageName = "batchRequest")]
    //[SoapDocumentMethod(
    //RequestElementName="batchRequest",
    //Use = SoapBindingUse.Literal)]
    //public XmlDocument BatchRequest(
    //  [XmlAttribute(AttributeName = "requestId")]
    //  string requestId,
    //  [XmlAttribute(AttributeName = "responseOrder")]
    //  string responseOrder,
    //  [XmlAttribute(AttributeName = "processing")]
    //  string processing,
    //  [XmlAttribute(AttributeName = "onError")]
    //  string onError,
    //  [XmlElement(ElementName="dsmlRequest", Form = XmlSchemaForm.Unqualified)]
    //  XmlNode[] Request
    //)
    //{
    //  XmlDocument dsmlRequest1 = new XmlDocument();

    //  XmlNode doc = dsmlRequest1.CreateNode(XmlNodeType.Element, "dsmlRequest", "");
    //  dsmlRequest1.AppendChild(doc);

    //  XmlNode o;

    //  foreach (XmlNode x in Request)
    //  {
    //    o = dsmlRequest1.ImportNode(x, true);
    //    dsmlRequest1.DocumentElement.AppendChild(o);
    //  }

    //  //try eagri extension
    //  string eagriDn = HttpContext.Current.Request.Headers["osso-user-dn"];

    //  if (!string.IsNullOrEmpty(eagriDn))
    //  {
    //    if (dsmlRequest1.DocumentElement.FirstChild.Name == "authRequest")
    //      //auth request exists, replace principal with eagri one
    //      dsmlRequest1.DocumentElement.FirstChild.Attributes["principal"].Value = eagriDn;

    //    else
    //    {
    //      //no authRequest present, create it
    //      XmlDocumentFragment docFrag = dsmlRequest1.CreateDocumentFragment();
    //      docFrag.InnerXml = "<authRequest principal='" + eagriDn + "'/>";
    //      dsmlRequest1.DocumentElement.InsertBefore(docFrag, dsmlRequest1.DocumentElement.FirstChild);
    //    }
    //  }
    //  //end of eagri ext

    //  LdRequestDocument rd;

    //  try
    //  {
    //    rd = LdRequestDocument.Parse(dsmlRequest1);
    //  }
    //  catch(Exception e)
    //  {
    //    throw new SoapException(e.Message, SoapException.ClientFaultCode);
    //  }

    //  LdResponseDocument response = LdRequestServerExtension.ProcessBatch(rd);

    //  return response.ToXml();

    //}


    //[MessageBodyMember(Name="batchRequest")]
    //XmlDocument batchRequest { get; set; }


  }

}
